<?php

namespace Console\Logic;

use Common\Logic\BaseLogic;

class TaskLogic extends BaseLogic
{
    public static $typeDic = array(
        '2' => '任务赚',
        // "3" => "分享赚",
        // "4" => "购物赚",
        // "5" => "信用赚"
    );

    private $listModel;

    public function __construct()
    {
        parent::__construct();
        $this->listModel = D('List');
    }

    /**
     * 返回类型字典.
     *
     * @return array
     */
    public function getTypeDic()
    {
        return static::$typeDic;
    }

    /**
     * 根据条件统计
     *
     * @param array $where
     *
     * @return mixed
     */
    public function statistics($where = array())
    {
        return $this->listModel->where($where)->count();
    }

    /**
     * 获取任务列表.
     *
     * @param int $page
     * @param int $size
     * @param $where
     *
     * @return mixed
     */
    public function getList($page = 1, $size = 20, $where = array())
    {
        $list = $this->listModel->where($where)->page($page, $size)->order('task_order desc,id desc')->select();
        $total = $this->statistics($where);

        return static::$res->suc('成功', array(
            'list' => $list,
            'total' => $total,
        ));
    }

    /**
     * 获取任务明细.
     *
     * @param $where
     *
     * @return mixed
     */
    public function getInfo($where)
    {
        $info = $this->listModel->where($where)->find();

        return static::$res->suc('成功', $info);
    }

    /**
     * 添加任务
     *
     * @param $userId
     * @param $phone
     * @param $data
     *
     * @return mixed
     */
    public function addTask($userId, $phone, $data)
    {
        if (empty($userId)) {
            return static::$res->err('userId不能为空');
        }
        if (empty($phone)) {
            return static::$res->err('添加人手机号不能为空');
        }

        static $fields = array(
            'type' => array('empty' => '任务类型不能为空'),
            'title' => array('empty' => '任务标题不能为空'),
            'cover' => array('empty' => '任务图片不能为空'),
            'begin_time' => array('empty' => '开始时间不能为空'),
            'end_time' => array('empty' => '结束时间不能为空'),
            'num' => array('empty' => '任务数量不能为空'),
            'income' => array('empty' => '参与价格不能为空'),
            'notice' => array('empty' => '任务须知不能为空'),
            'info' => array('empty' => '任务内容不能为空'),
            'task_order' => array(),
            'notice_url' => array(),
        );

        $fData = array();
        foreach ($fields as $title => $vo) {
            $val = $data[$title];
            if (!empty($vo['empty'])) {
                if (empty($val)) {
                    return static::$res->err($vo['empty']);
                }
            }
            $fData[$title] = trim($val);
        }

        $fData['income'] = $fData['income'] * 100;
        $fData['begin_time'] = date('Y-m-d H:i:s', strtotime($fData['begin_time']));
        $fData['end_time'] = date('Y-m-d H:i:s', strtotime($fData['end_time']));

        $fData['user_id'] = $userId;
        $fData['user_phone'] = $phone;
        $fData['add_time'] = date('Y-m-d H:i:s');
        $fData['task_state'] = 1;

        $id = $this->listModel->add($fData);
        if (empty($id)) {
            return static::$res->err('数据库异常，添加失败');
        }

        return static::$res->suc('成功');
    }

    /**
     * 编辑任务
     *
     * @param $userId
     * @param $id
     * @param $data
     *
     * @return mixed
     */
    public function editTask($userId, $id, $data)
    {
        if (empty($userId)) {
            return static::$res->err('userId不能为空');
        }
        if (empty($id)) {
            return static::$res->err('任务ID不能为空');
        }

        static $fields = array(
            'type' => array('empty' => '任务类型不能为空'),
            'title' => array('empty' => '任务标题不能为空'),
            'cover' => array('empty' => '任务图片不能为空'),
            'begin_time' => array('empty' => '开始时间不能为空'),
            'end_time' => array('empty' => '结束时间不能为空'),
            'num' => array('empty' => '任务数量不能为空'),
            'income' => array('empty' => '参与价格不能为空'),
            'notice' => array('empty' => '任务须知不能为空'),
            'info' => array('empty' => '任务内容不能为空'),
            'task_order' => array(),
            'notice_url' => array(),
            'test_user' => array(),
        );

        $fData = array();
        foreach ($fields as $title => $vo) {
            $val = $data[$title];
            if (!empty($vo['empty'])) {
                if (empty($val)) {
                    return static::$res->err($vo['empty']);
                }
            }
            $fData[$title] = trim($val);
        }

        $fData['income'] = $fData['income'] * 100;
        $fData['begin_time'] = date('Y-m-d H:i:s', strtotime($fData['begin_time']));
        $fData['end_time'] = date('Y-m-d H:i:s', strtotime($fData['end_time']));

        $fData['update_user'] = $userId;
        $fData['update_time'] = date('Y-m-d H:i:s');

        $coum = $this->listModel->where(array(
            'id' => $id,
        ))->save($fData);
        if (empty($coum)) {
            return static::$res->err('数据库异常，编辑失败');
        }

        return static::$res->suc('成功');
    }

    /**
     * 删除一个任务
     *
     * @param $userId
     * @param $id
     * @param string $msg
     *
     * @return mixed
     */
    public function delTask($userId, $id, $msg = '发布者删除')
    {
        if (empty($userId)) {
            return static::$res->err('userId不能为空');
        }
        if (empty($id)) {
            return static::$res->err('任务ID不能为空');
        }

        $data = array(
            'task_state' => -1,
            'end_time' => date('Y-m-d H:i:s'),
            'update_time' => date('Y-m-d H:i:s'),
            'del_msg' => $msg,
        );

        $coum = $this->listModel->where(array(
            'id' => $id,
        ))->save($data);
        if (empty($coum)) {
            return static::$res->err('数据库异常，删除失败');
        }

        return static::$res->suc('删除成功');
    }

}
